package kr.co.mcshop.qna;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import kr.co.mcshop.qnacomment.QnAComment;
import kr.co.mcshop.qnacomment.QnACommentService;
import kr.co.mcshop.qnareply.QnAReply;
import kr.co.mcshop.qnareply.QnAReplyService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping(value = "/QnA")
@Controller
public class QnAController {
	private static final Logger logger = LoggerFactory
			.getLogger(QnAController.class);

	@Autowired
	private QnAService qnaService;

	@Autowired
	private QnACommentService qnacommentService;

	@Autowired
	private QnAReplyService qnareplyService;

	@RequestMapping(value = "/QnA_main", method = RequestMethod.GET)
	public String qnamain(Model model, @RequestParam Map map) {
		List<QnA> list = qnaService.selectqna(map);
		model.addAttribute("qnaList", list);
		return "QnA_main";
	}

	@RequestMapping(value = "/QnA_detail", method = RequestMethod.GET)
	public String qna_detail(Model model, @RequestParam Map map) {

		qnaService.updateReadCount(map);
		List<QnA> list = qnaService.selectqna(map);
		model.addAttribute("qnaList", list.get(0));

		ArrayList<QnAComment> list1 = qnacommentService.selectqnacomment(map);
		model.addAttribute("commentList", list1);

		ArrayList<QnAReply> list2 = new ArrayList<QnAReply>();

		for (int i = 0; i < list1.size(); i++) {
			//System.out.println("selectReply" + qnareplyService.selectreply(list1.get(i).getComment_no()));
			list2.addAll(qnareplyService.selectreply(list1.get(i).getComment_no()));		
		}
		model.addAttribute("replyList", list2);
		System.out.println("comment" + list1);
		System.out.println("comment_reply" + list2);
		return "QnA_detail";
	}

	@RequestMapping(value = "/QnAInsertForm", method = RequestMethod.GET)
	public String QnAInsertForm(Model model) {
		return "QnA_write";
	}

	@RequestMapping(value = "/QnAInsert", method = RequestMethod.POST)
	public String QnAInsert(Model model, @RequestParam Map map) {
		System.out.println(map);
		qnaService.insertqna(map);
		return "redirect:QnA_main";
	}

	@RequestMapping(value = "QnADelete", method = RequestMethod.GET)
	public String delete(Model model, @RequestParam Map map) {
		qnaService.deleteqna(map);
		qnacommentService.FAQcommentDelete(map);
		qnareplyService.FAQreplyDelete(map);
		return "redirect:QnA_main";
	}

	@RequestMapping(value = "/QnAUpdateForm", method = RequestMethod.GET)
	public String QnAUpdateForm(Model model, @RequestParam Map map) {
		List<QnA> list = qnaService.selectqna(map);
		model.addAttribute("qnaList", list.get(0));
		return "QnA_update";
	}

	@RequestMapping(value = "/QnAUpdate", method = RequestMethod.POST)
	public String QnAUpdate(Model model, @RequestParam Map map) {
		qnaService.updateqna(map);
		return "redirect:QnA_main";
	}

	@RequestMapping(value = "/NewFile")
	public String NewFile() {
		return "NewFile";
	}

	@RequestMapping(value = "/search")
	public String search(String board_title, Model model, @RequestParam Map map) {
	    ArrayList<QnA> list = new ArrayList<QnA>();
		if (map.get("searchtxt") == null) {
			return "redirect:QnA_main";
		}
		if(map.get("type_cd").equals("0")){
			map.put("searchtxt", map.get("searchtxt"));
			List<QnA> titleSearchResult = qnaService.searchtitle(map);
			
			for(QnA qnalist : titleSearchResult){
				list.add(qnalist);
			}
		}else if(map.get("type_cd").equals("1")){
			map.put("searchtxt", map.get("searchtxt"));
			List<QnA> userIdSearchResult = qnaService.searchuserid(map);
			
			for(QnA qnalist : userIdSearchResult){
				list.add(qnalist);
			}
		}
		
		model.addAttribute("qnaList", list);
		return "QnA_main";
	}

	/* @RequestMapping(value = "commentInsert", method = RequestMethod.POST)
	 * public String commentInsert(Comment comDto, HttpServletRequest request) {
	 * System.out.println(comDto); ibaties.commentInsert(comDto); 
	 * return "board_detail"; }
	 */

}
